﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECRMS.Biz.Dom;
using ECRMS.Data;
using System.Data.Linq;
using ECRMS.Common;

namespace ECRMS.Biz
{
    public class CtPostLogic
    {
        //公司新闻
        public CtServiceResult<List<CtPost>> GetAllCorpPostsByCorpId(Guid corpId)
        {
            using (var dataContext = CSRManagerSystemDBDataContext.NewInstance())
            {
                try
                {
                    return new CtServiceResult<List<CtPost>>
                    {

                        Data = (from a in dataContext.GetTable<CSRPost>()
                               join b in dataContext.GetTable<CSRLogin>() on a.CreateUserRefId equals b.UserRefId
                               join c in dataContext.GetTable<CSRUser>() on b.UserRefId equals c.UserRefId
                               where c.CorpRefId == corpId && !a.IsDisabled  && a.CategoryId==(int)PostTypeEnum.CropInfo
                               select new CtPost { 
                                CategoryId=a.CategoryId,
                                Content=a.Content,
                                CreateDate=a.CreateDate,
                                CreateUserRefId=a.CreateUserRefId,
                                CreatorName=c.Name,
                                PostRefId=a.PostRefId,
                                Subject=a.Subject,
                                IsDisabled=a.IsDisabled
                               }).ToList(),
                        Status=1

                    };
                }
                catch (Exception)
                {
                    return new CtServiceResult<List<CtPost>>
                    {
                        Message = "try_catch_exception",
                        Status = 0
                    };
                }
            }
        }

        //新增、修改、删除
        public CtServiceResult UpdateCorpPost(CtPost post)
        {
            using (var dataContext = CSRManagerSystemDBDataContext.NewInstance())
            {
                try
                {
                    CSRPost pst = dataContext.GetTable<CSRPost>().FirstOrDefault(p => p.PostRefId == post.PostRefId);

                    if (pst == null)//新增
                    {
                        pst = new CSRPost
                        {
                            CategoryId = (int)PostTypeEnum.CropInfo,
                            Content = post.Content,
                            CreateDate = DateTime.Now,
                            CreateUserRefId = post.CreateUserRefId,
                            IsDisabled = false,
                            PostRefId = post.PostRefId,
                            Subject = post.Subject
                        };
                        dataContext.GetTable<CSRPost>().InsertOnSubmit(pst);
                    }
                    else if (post != null && post.IsDisabled)//删除
                    {
                        pst.IsDisabled = post.IsDisabled;
                        pst.CreateDate = DateTime.Now;
                    }
                    else if (pst != null)//修改
                    {

                        pst.Content = post.Content;
                        pst.CreateDate = DateTime.Now;
                    }
                    dataContext.SubmitChanges();
                    return new CtServiceResult
                    {
                        Status=1
                    };
                }
                catch (Exception)
                {
                    return new CtServiceResult
                    {
                        Message = "try_catch_exception",
                        Status = 0
                    };
                }
            }
        }
    }
}
